<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/30/0030
 * Time: 15:26
 */

namespace app\sports_admin\model;


use think\Model;

class SportsProject extends Model
{
    protected $table = 'table_project';
    protected $pk = 'id';
    protected $createTime = 'add_time';
    protected $updateTime = 'update_time';
    protected $autoWriteTimestamp = true;
    protected $hidden = ['update_time'];

    public function getAddTimeAttr($value)
    {
        return date('Y-m-d H:i:s',$value);
    }
    /**
     * 获取体育项目列表
     * @param $page 第几页
     * @param $rows 每页显示的条数
     * @param $sort 要排序的字段
     * @param $sortOrder 升序/降序
     * @param $searchName 要检索的字段
    */
    public static function getProjectList($page,$rows,$sort,$sortOrder,$searchName)
    {
        $result = self::where(function($query)use($searchName){
            if($searchName){
                $query->where('title','like',"%{$searchName}%");
            }
        })->order("{$sort}","{$sortOrder}")->page($page)->limit($rows)->select();
        $count = self::where(function($query)use($searchName){
            if($searchName){
                $query->where('title','like',"%{$searchName}%");
            }
        })->count();
        return json(['rows'=>$result,'total'=>$count]);
    }
    /**
     * 添加体育项目
     * @param $title 项目名称
     * @param $company 单位
     */
    public static function projectInsert($title,$company)
    {
        return self::create([
            'title'=>$title,
            'company'=>$company
        ]);
    }

    /**
     * 更新项目信息
     * @param $id 项目信息id
     * @param $title 项目名称
     * @param $company 单位
     */
    public static function projectUpdate($id,$title,$company)
    {
        return self::where('id',$id)->update([
            'title'=>$title,
            'company'=>$company
        ]);
    }
}